// MINI UI解析
mini.parse();

/**
 * 服务端配置处理器类
 */
function ServerCfgHandler() {

}

// 是否启用积分功能数据
ServerCfgHandler.sscIntegralData = [
	{text: "启用",value: 1},
	{text: "禁用",value: 0}
];
mini.get("sscIntegral").setData(ServerCfgHandler.sscIntegralData);

// 是否启用等级功能数据
ServerCfgHandler.sscGradeData = [
	{text: "启用",value: 1},
	{text: "禁用",value: 0}
];
mini.get("sscGrade").setData(ServerCfgHandler.sscGradeData);

/**
 * 加载服务端配置数据
 */
ServerCfgHandler.loadSysServerCfg = function(){
	$.ajax({
		url : __root + "/getSysServerCfg.do",
		cache : false,
		dataType : 'json',
		success : function(data) {
			if(data != "" && data != "[]"){
				// 商品缩略图
				mini.get("sscGswidth").setValue(data.sscGswidth);
				mini.get("sscGsheight").setValue(data.sscGsheight);
				mini.get("sscGmwidth").setValue(data.sscGmwidth);
				mini.get("sscGmheight").setValue(data.sscGmheight);
				mini.get("sscGbwidth").setValue(data.sscGbwidth);
				mini.get("sscGbheight").setValue(data.sscGbheight);
				
				// 会员等级身份保持时间
				mini.get("sscLasttime").setValue(data.sscLasttime);
				// 隐性ID
				mini.get("sscId").setValue(data.sscId);
				mini.get("sscIntegral").setValue(data.sscIntegral);
				mini.get("sscGrade").setValue(data.sscGrade);
			}
		},
		error: function(){
			MiniuiTools.miniError("系统提示","数据加载出错！");
		}
	});
}

ServerCfgHandler.loadSysServerCfg();

/**
 * 验证配置数据
 * @param {} gsw
 * @param {} gsh
 * @param {} gmw
 * @param {} gmh
 * @param {} gbw
 * @param {} gbh
 * @param {} lst
 * @return {Boolean}
 */
ServerCfgHandler.validFormData = function(gsw, gsh, gmw, gmh, gbw, gbh, lst){
	gsw.validate();
	if (!gsw.isValid()) return false;
	
	gsh.validate();
	if (!gsh.isValid()) return false;
	
	gmw.validate();
	if (!gmw.isValid()) return false;
	
	gmh.validate();
	if (!gmh.isValid()) return false;
	
	gbw.validate();
	if (!gbw.isValid()) return false;
	
	gbh.validate();
	if (!gbh.isValid()) return false;
	
	lst.validate();
    if (!lst.isValid()) return false;
    
    return true;
}

/**
 * 保存编辑
 */
ServerCfgHandler.save = function(){
	var sscId = mini.get("sscId");
	var gsw = mini.get("sscGswidth");
	var gsh = mini.get("sscGsheight");
	var gmw = mini.get("sscGmwidth");
	var gmh = mini.get("sscGmheight");
	var gbw = mini.get("sscGbwidth");
	var gbh = mini.get("sscGbheight");
	var lst = mini.get("sscLasttime");
	var ial = mini.get("sscIntegral");
	var gde = mini.get("sscGrade");
	
	if(!ServerCfgHandler.validFormData(gsw, gsh, gmw, gmh, gbw, gbh, lst)) return;
					
	mini.confirm("确认保存新配置" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var formData = {};
					formData.sscId = sscId.getValue();
					formData.sscGswidth = gsw.getValue();
					formData.sscGsheight = gsh.getValue();
					formData.sscGmwidth = gmw.getValue();
					formData.sscGmheight = gmh.getValue();
					formData.sscGbwidth = gbw.getValue();
					formData.sscGbheight = gbh.getValue();
					formData.sscLasttime = lst.getValue();
					formData.sscIntegral = ial.getValue();
					formData.sscGrade = gde.getValue();
					
                    $.ajax({
                        url: __root + "/saveSysServerCfg.do",
                        data: {
				        	sscInfo: mini.encode(formData),
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","保存成功！", function(action){
				        		ServerCfgHandler.loadSysServerCfg();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	MiniuiTools.miniError("系统提示", "保存失败！" + jqXHR.responseText);
                        }
                    });	
                } 
			}
		)
}